package com.qc.controller.web;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qc.entity.SecretaryEntity;
import com.qc.pageUtil.page.page.Page;
import com.qc.pageUtil.page.page.PageModel;
import com.qc.pageUtil.page.page.PageModelList;
import com.qc.pageUtil.page.vo.NewsAndNav;
import com.qc.service.SecretaryService;

@SuppressWarnings({ "rawtypes", "unchecked" })
@Controller
@RequestMapping("/web")
public class WebSecretaryController {

	@Resource(name="secretaryService")
	private SecretaryService secretaryService;
	
	/**
     * 跳转所有秘书一级到首页
     * @return
     */
    @RequestMapping(value = "/toSecretaryIndex", produces = "text/html;charset=UTF-8")
    public ModelAndView toSecretaryIndex() {
        ModelAndView mav = new ModelAndView("/sec/leibie_seczy");
        
        List<Map<String, Object>> list = secretaryService.getAllLevleOne();
        mav.addObject("list",list);
        
        
        return mav;
    }
    
    /**
     * 跳转所有秘书二级到首页
     * @return
     */
    @RequestMapping(value = "/toSecretaryIndex2", produces = "text/html;charset=UTF-8")
    public ModelAndView toEducationIndex2(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView("/sec/leibie_seczy2");
        
        String parent = request.getParameter("parent");
        
        List<Map<String, Object>> list = secretaryService.getAllLevleOne();
        mav.addObject("list",list);
        mav.addObject("parent",parent);
        
        
        return mav;
    }
    
    /**
     * 所有秘书一级数据展示
     * @param request
     * @return
     */
    @RequestMapping(value = "/toListAllSecretary", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String toListAllSecretary(HttpServletRequest request) {
        
        String name = request.getParameter("name");
        //当前页面
        int pageNo = Integer.parseInt(request.getParameter("pageNo"));
        //每页显示多少条
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));
        
        //根据条件总数
        int recordCount = secretaryService.getListSecretaryCount(name);
        
        //根据条件查询当前页数据
        List<SecretaryEntity> list = secretaryService.getAllSecretary(name, pageNo, pageSize);
        
        //分页查询公共类
        Page<SecretaryEntity> page = new Page<SecretaryEntity>(pageNo, recordCount,pageSize);
        page.setList(list);//设置结果集  
        PageModelList<SecretaryEntity> pageModel = new PageModelList<SecretaryEntity>(page, "#", PageModel.NUM_MODEL,"doPage","1");//设置页码显示类型  
        //4 组装带翻页导航功能的对象
        NewsAndNav  objs = new NewsAndNav(list, pageModel.getPageModel());
        
        return JSONObject.fromObject(objs).toString();
        
    }
    
    /**
     * 所有秘书一级添加
     */
    @RequestMapping(value = "/toSaveSecretary")
    @ResponseBody
    public int toSaveSecretary(HttpServletRequest request) {
        
        String name = request.getParameter("name");
        
        SecretaryEntity secretaryEntity = new SecretaryEntity();
        
        secretaryEntity.setName(name);
        secretaryEntity.setParent("");
        secretaryEntity.setLevel("1");
        
        int flag = secretaryService.saveSecretary(secretaryEntity);
        
        return flag;
    }
    
    /**
     * 所有秘书一级删除
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/toDeleteSecretaryId")
    @ResponseBody
    public int toDeleteSecretaryId(HttpServletRequest request,HttpServletResponse response) {
        long id = Long.parseLong(request.getParameter("id"));
        int flag = secretaryService.deleteSecretaryId(id);
        return flag;
    }
    
    /**
     * 所有秘书一级修改
     */
    
    @RequestMapping(value = "/toUpdateSecretaryId")
    @ResponseBody
    public int toUpdateSecretaryId(HttpServletRequest request, HttpServletResponse response) {
        long id = Long.parseLong(request.getParameter("id"));
        String name = request.getParameter("name");
        double price = Double.parseDouble(request.getParameter("price"));
        
        SecretaryEntity secretaryEntity = new SecretaryEntity();
        
        secretaryEntity.setName(name);
        secretaryEntity.setPrice(price);
        secretaryEntity.setId(id);
        
        int flag = secretaryService.updateSecretaryId(secretaryEntity);
        return flag;
    }
    
    /**
     * 所有秘书二级添加
     */
    @RequestMapping(value = "/toSaveSecretary2")
    @ResponseBody
    public int toSaveSecretary2(HttpServletRequest request) {
        
        String name = request.getParameter("name");
        String parent = request.getParameter("parent");
        double price = Double.parseDouble(request.getParameter("price"));
        
        SecretaryEntity secretaryEntity = new SecretaryEntity();
        
        secretaryEntity.setName(name);
        secretaryEntity.setParent(parent);
        secretaryEntity.setPrice(price);
        secretaryEntity.setLevel("2");
        
        int flag = secretaryService.saveSecretary(secretaryEntity);
        
        return flag;
    }
    
    /**
     * 所有秘书一级数据展示
     * @param request
     * @return
     */
    @RequestMapping(value = "/toListAllSecretaryTwo", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String toListAllSecretaryTwo(HttpServletRequest request) {
        
        String name = request.getParameter("name");
        
        //上一级属性
        String parent = request.getParameter("parent");
        
        //当前页面
        int pageNo = Integer.parseInt(request.getParameter("pageNo"));
        //每页显示多少条
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));
        
        //根据条件总数
        int recordCount = secretaryService.getListSecretaryCountTwo(name,parent);
        
        //根据条件查询当前页数据
        List<SecretaryEntity> list = secretaryService.getAllSecretaryTwo(name,parent, pageNo, pageSize);
        
        //分页查询公共类
        Page<SecretaryEntity> page = new Page<SecretaryEntity>(pageNo, recordCount,pageSize);
        page.setList(list);//设置结果集  
        PageModelList<SecretaryEntity> pageModel = new PageModelList<SecretaryEntity>(page, "#", PageModel.NUM_MODEL,"doPage","1");//设置页码显示类型  
        //4 组装带翻页导航功能的对象
        NewsAndNav  objs = new NewsAndNav(list, pageModel.getPageModel());
        
        return JSONObject.fromObject(objs).toString();
        
    }
	
}
